package com.sijiao.difflog.configuration;

import com.sijiao.difflog.aspect.LogAspect;
import com.sijiao.difflog.service.IReporterService;
import com.sijiao.difflog.service.ISearchService;
import com.sijiao.difflog.service.impl.ReporterServiceImpl;
import com.sijiao.difflog.service.impl.SearchServiceImpl;
import com.sijiao.difflog.util.ContextBeanFactoryUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import java.util.concurrent.*;

@Configuration
@Import({ContextBeanFactoryUtil.class, LogAspect.class})
public class LogAutoConfiguration {
    @Value("${log.reporter.threadpool.core}")
    private int core;
    @Value("${log.reporter.threadpool.max}")
    private int max;

    @Bean
    public IReporterService reporterService() {
        return new ReporterServiceImpl(threadPoolExecutor());
    }
    @Bean
    public ISearchService searchService() {
        return new SearchServiceImpl();
    }

    @Bean
    public ThreadPoolExecutor threadPoolExecutor() {
        return new ThreadPoolExecutor(core, max, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<>(1), Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardPolicy());
    }
}
